package vh.android.multiconnector.quiz.ui.adapters;

import java.util.ArrayList;

import vh.android.multiconnector.quiz.R;
import vh.android.multiconnector.quiz.models.GameAnswer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class QuestionAdapter extends BaseAdapter {

	private ArrayList<GameAnswer> mQuestions = new ArrayList<GameAnswer>();
	private Context mContext;

	public QuestionAdapter(Context pContext) {
		mContext = pContext;
	}

	@Override
	public int getCount() {
		return mQuestions.size();
	}

	@Override
	public GameAnswer getItem(int pIndex) {
		return mQuestions.get(pIndex);
	}

	@Override
	public long getItemId(int pIndex) {
		return pIndex;
	}

	@Override
	public View getView(int pIndex, View pView, ViewGroup pViewGroup) {
		GameAnswer answer = getItem(pIndex);

		if (pView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			pView = inflater.inflate(R.layout.row_question, null);
		}

		TextView tvAnswer = (TextView) pView.findViewById(R.id.tvAnswer);
		TextView tvUser = (TextView) pView.findViewById(R.id.tvUser);
		tvAnswer.setText(answer.getAnswer());
		tvUser.setText(answer.getUsers());

		return pView;
	}

	public void addAll(GameAnswer[] gameAnswers) {
		for (GameAnswer a : gameAnswers)
			mQuestions.add(a);
		notifyDataSetChanged();
	}
}
